package com.zhong.gang.app.utils.waidiaoimg;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.zhong.gang.applib.ui.preview.PreviewAct;
import java.util.ArrayList;

public class WaiDiaoImgView extends FrameLayout implements BaseQuickAdapter.OnItemClickListener {
    private WaidiaoImgAdapter adapter;
    public WaiDiaoImgView(@NonNull Context context) {
        super(context);
        initView();
    }

    public WaiDiaoImgView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public WaiDiaoImgView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }
    private void initView(){
        RecyclerView recyclerView = new RecyclerView(getContext());
        recyclerView.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT));
        recyclerView.setLayoutManager(new GridLayoutManager(getContext(),2));
        recyclerView.setAdapter(adapter = new WaidiaoImgAdapter());
        adapter.setOnItemClickListener(this);
        this.addView(recyclerView);
    }

    public void addItem(WaidiaoImgAdapter.WaiDiaoImgBean bean){
        if (bean == null)
            return;
        adapter.addData(bean);
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        WaidiaoImgAdapter.WaiDiaoImgBean bean = (WaidiaoImgAdapter.WaiDiaoImgBean) adapter.getItem(position);
        ArrayList<String> data = new ArrayList<>();
        data.add(bean.getImg());
        PreviewAct.openAct(getContext(),data,0);
    }
}
